#ifndef  _CCSocketHandler_H_
#define _CCSocketHandler_H_
#include "cache.h"
#include "ICC_Decoder.h"
#include "ICC_TCP_Handler.h"
#include "PacketBase.h"
#include "socketNet.h"
#include "wspack.h"

using namespace comm::sockcommu;

#define MAX_WEB_RECV_LEN            102400

enum CConnState
{
	CONN_IDLE,
	CONN_FATAL_ERROR,
	CONN_DATA_ERROR,
	CONN_CONNECTING,
	CONN_DISCONNECT,
	CONN_CONNECTED,
	CONN_DATA_SENDING,
	CONN_DATA_RECVING,
	CONN_SEND_DONE,
	CONN_RECV_DONE,
	CONN_APPEND_SENDING,
	CONN_APPEND_DONE,
	CONN_XML_POLICY
};
enum connType
{
	CONN_TYPE_NONE = 0,
	CONN_TYPE_CONN = 1,
	CONN_TYPE_ACCEPT = 2
};

enum dataProtocol
{
	DATA_PROTOCOL_UNKNOWN = 0,
	DATA_PROTOCOL_TCP = 1,
	DATA_PROTOCOL_WEBSOCKET = 2,
	DATA_PROTOCOL_HTTP = 3,
};

class CCSocketHandler : public ICC_TCP_Handler
{
public:
	CCSocketHandler();
	virtual ~CCSocketHandler();

public:
	virtual int Init();
	virtual int InputNotify();
	virtual int OutputNotify();
	virtual int HangupNotify();
	virtual int RecvData(char buff[], int buff_size, int time_out);
	int Send(const char * buff, int len);
	virtual int Send(CCPacketBase* pOutPkg)=0;
	inline void setId(const unsigned int id){ _id = id; }
	inline unsigned int getId(){ return _id; }
	inline void setType(const unsigned short type){ _type = type; }
	inline unsigned short getType(){ return _type; }
	inline void setPropertyPtr(void* ptr){ _property_ptr = ptr; }
	inline void* getPropertyPtr(){ return _property_ptr; }
	inline void setPropertyInt32(int u32){ _property_u32 = u32; }
	inline int getPropertyInt32(){ return _property_u32; }
    virtual connType getConnType(void)=0;
	virtual dataProtocol getDataProtocol(void);
	void setDataProtocol(dataProtocol prot);
	bool isWshandshake(void);
	void setWshandshake(bool handshake);
	void Destroy();

	inline unsigned int addTick(void) { return ++m_tick; }
	inline void resetTick(void) { m_tick = 0; }
	inline unsigned int getTick(void) { return m_tick; }
public:
	virtual int OnClose();
	virtual int OnConnected();
	virtual int OnPacketComplete(const char * data, int len);
	virtual ICC_Decoder*  CreateDecoder() = 0;
	void Reset();
public:
	int Connect();
protected:
	ICC_Decoder* _decode;
	CConnState	_stage;

public:
	int handle_input();
	int handle_output();
	int handle_close();

	CRawCache       _r;
	CRawCache       _w;

public:
	unsigned int _id;
	unsigned short _type;
	void* _property_ptr;
	int _property_u32;
	bool _isDel;
private:
    dataProtocol m_dprot;
	bool m_wshandshake;
	CWspack m_wsrPack;
	unsigned int m_tick;
};

typedef  CCSocketHandler  SocketHandler;

#endif

